// 调取第三方短信接口
let request = require('request');
let sms = function smsCode(phone){
      return new Promise((resolve,reject)=>{
      let telCode = '';
      for (var i = 0; i < 6; i++) {
        var radom = Math.floor(Math.random() * 10);
        telCode += radom;
      }
      let str = encodeURI(`验证码：${telCode}如非本人操作，请忽略本短信【味多美蛋糕】`);
      let url = `https://imlaixin.cn/Api/send/data/json?accesskey=7318&secretkey=9e0770113ddfd12ea7e30c6a0c939999496c0afd&mobile=${phone}&content=${str}`;
      // console.log('调用第三方接口',url);
      // 调用短信发送接口
      request(url, function (error, response, body) {
        if(error){
          reject(error)
        }else{
          if (!error && response.statusCode == 200) {
            // console.log("第三方接口返回",response.body) // 请求成功的处理逻辑，注意body是json字符串
          if (JSON.parse(response.body).result == '01') {
            resolve(telCode);
          }
        }
        }
    })
  })
}


// 暴露
module.exports = sms;